Imports System.Diagnostics

Partial Public Class MainForm
    Inherits Form
    Private exiting As Boolean = False
    Public Sub New()
        InitializeComponent()

        idleThresholdNumericUpDown.Value = userTimer.IdleThreshold / 1000
    End Sub

    ''' <summary>
    ''' Handles the Click event for the Reset button.  Resets the elapsed time ActivityTimer object to zero.
    ''' </summary>
    Private Sub resetButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles resetButton.Click
        userTimer.Reset()
    End Sub

    ''' <summary>
    ''' Handles the Tick event of the updateTimer object to update the UI with active time
    ''' </summary>
    Private Sub updateTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles updateTimer.Tick
        Dim ts As TimeSpan = userTimer.ActiveTime

        ' Not necessary to update the status label since the active/idle events to it
        statusLabel.Text = userTimer.UserActiveState.ToString()

        Dim totalActive As String = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)

        timerLabel.Text = String.Format("{0} since {1}", totalActive, userTimer.LastResetTime.ToShortTimeString())

        appNotifyIcon.Text = String.Format("Coding 4 Fun - User Activity - Active {0}", totalActive)
    End Sub

    ''' <summary>
    ''' Handles the Click event of the Enable menu item.  Enables/disables the ActivityTimer
    ''' </summary>
    Private Sub enableToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles enableToolStripMenuItem.Click
        enableToolStripMenuItem.Checked = Not enableToolStripMenuItem.Checked

        userTimer.Enabled = enableToolStripMenuItem.Checked
    End Sub

    ''' <summary>
    ''' Handles the Click event of the Reset menu item.  Resets the elapsed time ActivityTimer object to zero. 
    ''' </summary>
    Private Sub resetTimerToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles resetTimerToolStripMenuItem.Click
        userTimer.Reset()
    End Sub

    ''' <summary>
    ''' Handles the Click event of the Quit menu item.  Exits the application
    ''' </summary>
    Private Sub quitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles quitToolStripMenuItem.Click
        appNotifyIcon.Visible = False
        exiting = True
        Me.Close()
    End Sub

    ''' <summary>
    ''' Handles the ValueChanged event of the IdleThresholdNumericUpDown control.  Changes the 
    ''' number of seconds until inactivity considers the user to be considered Idle.
    ''' </summary>
    Private Sub idleThresholdNumericUpDown_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles idleThresholdNumericUpDown.ValueChanged
        userTimer.IdleThreshold = CLng(idleThresholdNumericUpDown.Value) * 1000
    End Sub

    ''' <summary>
    ''' Handles the UserIdleEvent of the ActivityTimer class.  Updates the UI for the Active state
    ''' </summary>
    Private Sub userTimer_UserActiveEvent(ByVal sender As Object, ByVal e As EventArgs) Handles userTimer.UserActiveEvent
        If Me.InvokeRequired Then
            Me.Invoke(DirectCast(AddressOf SetStatusLabelActive, MethodInvoker))
            Return
        Else
            statusLabel.Text = "UserActiveEvent"
        End If
    End Sub

    ''' <summary>
    ''' Because VB does not support anonymous methods, and MethodInvoker only supports
    ''' no-argument methods, you need to create stubs to call.  This is cleaner in C#.
    ''' </summary>
    Private Sub SetStatusLabelActive()
        userTimer_UserActiveEvent(Nothing, Nothing)
    End Sub

    ''' <summary>
    ''' Handles the UserIdleEvent of the ActivityTimer class.  Updates the UI for the Idle state.
    ''' </summary>
    Private Sub userTimer_UserIdleEvent(ByVal sender As Object, ByVal e As EventArgs) Handles userTimer.UserIdleEvent
        If Me.InvokeRequired Then
            Me.Invoke(DirectCast(AddressOf SetStatusLabelIdle, MethodInvoker))
            Return
        Else
            statusLabel.Text = "UserIdleEvent"
        End If
    End Sub

    ''' <summary>
    ''' Because VB does not support anonymous methods, and MethodInvoker only supports
    ''' no-argument methods, you need to create stubs to call.  This is cleaner in C#.
    ''' </summary>
    Private Sub SetStatusLabelIdle()
        userTimer_UserIdleEvent(Nothing, Nothing)
    End Sub

    ''' <summary>
    ''' Handles the MouseDoubleClick event when the user double-clicks the notification icon
    ''' </summary>
    Private Sub appNotifyIcon_MouseDoubleClick(ByVal sender As Object, ByVal e As MouseEventArgs) Handles appNotifyIcon.MouseDoubleClick
        Me.Visible = True
    End Sub

    ''' <summary>
    ''' Handles the FormClosing event to hide the form or allow it to close
    ''' </summary>
    Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' If exiting is set, application is intended to exit.  Otherwise,
        ' just hide the form and discard the event.
        If Not exiting Then
            Me.Visible = False
            e.Cancel = True
        End If
    End Sub


End Class
